//横向堆叠柱状图--异常
function DRAWCROSSSTACK(url,modelSend,drawContainer,drillValue,drillUrl) {
    this.draw = function () {
        console.log(modelSend);
        $.ajax({ type: "post",
            url: url,
            dataType: "json",
            data: {"paramData":JSON.stringify(modelSend)},
            success: function (result) {
                 console.log(result);
                var str="";
                if(result.front) {
                    for (var i = 0; i < result.front.length; i++) {
                        str += '<p class="titleSub">' + result.front[i].label + '' +
                            '<span>' + result.front[i].value + '</span></p>';
                    }
                }
                $("#text_h4").html(result.titledate+"月");
                $("#summarizing").html(str);
                var dcInstance= new DRAWCCHART(result,drawContainer,drillValue,drillUrl,JSON.stringify(modelSend));
                dcInstance.draw(result,drawContainer,drillValue,drillUrl,JSON.stringify(modelSend));
            },
            error: function (err) {
                console.log(err);
            }});
    };
};
//定义一个用于绘制图形的类
function DRAWCCHART(json,drawContainer,drillValue,drillUrl,modelSend){
    //坐标轴的颜色
    this.colorg=['#f00','#0f0','#00f'];
    //根据得到的数据绘制Y轴
    this.setYaxis=function(data){
        console.log(data);
        var yaxArray=[];
        for(var i=0;i<data.axis.yCnt;i++){
            var k=i;
            var obj={labels:{format:'{value}',style:{color:''}},title:{text:''}};
            obj.labels.style.color =this.colorg[k];
            if(data.theme.yAxis[k].align=='right'||data.theme.yAxis[k].opposite=='false'){
                obj.labels.format='{value}%';
                obj.opposite='true';
            }
            yaxArray.push(obj);
        }
        // console.log(yaxArray);
        return yaxArray;
    };
    //根据得到的数据设置指标
    this.setSeries=function(dataSource){
        var serArray=[];
        console.log(dataSource)
        for(var key in dataSource.data){
            var obj={};
            var mun=dataSource.data[key].yAxis;
            obj.type=dataSource.theme.yAxis[mun].type;
            //console.log(obj.data);
            obj.name=dataSource.data[key].label;
            obj.data=dataSource.data[key].value;
            //console.log(obj.data);
            obj.color=this.colorg[key];
            obj.zIndex=dataSource.theme.yAxis[mun].type=='line'?1:0;
            obj.yAxis=dataSource.data[key].yAxis;
            obj.dataLabels = {
                    enabled: true,
                    color: '#000000'
            };
            obj.test=dataSource.data[0].test;
            serArray.push(obj);
        }
        return serArray;
    };
    //绘制堆叠柱状图的方法
    this.draw=function(json,container){
        var _series_newdata = new Array()
        $.each(
            json.data[0].test,
            function (i, d) {
                _series_newdata.push({ color: '#f9e4be', y: d, stack: i });
            });
        console.log(_series_newdata);
        var option={
            chart: {
                type: 'bar'   //横向堆图
            },
            title: {
                text: ''     // 标题
            },
            xAxis: {
                categories: json.axis.x   // x 轴分类
            },
            yAxis:this.setYaxis(json),
            tooltip: {
                pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.2f}%)<br/>',
                shared: true
            },
            plotOptions: {
                series: {
                    stacking: 'normal',
                    cursor:'pointer',
                    dataLabels: {
                        useHTML: true,
                        color: '#4f4f4f',
                        style: {
                            fontFamily:'宋体',
                            fontWeight:'normal',
                            fontSize: '10px',
                        },
                        formatter: function () {
                            return  _series_newdata[this.point.x].y;
                        }
                    },
                    point:{
                        events:{
                            click:function(e){
                                if(drillValue) {
                                    var lab=e.point.category;
                                    var str=drillUrl+'?lab='+lab+'&modelSend='+modelSend;
                                    window.open(str,'_self');
                                }
                            }
                        }
                    }
                }
            },
            legend:{
                align: 'center',
                verticalAlign: 'top'
            },
            credits: {//隐藏官网提示
                enabled: false
            },
            exporting: {
                chartOptions: {
                    plotOptions: {
                        series: {
                            dataLabels: {
                                enabled: true
                            }
                        }
                    }
                }
            },
            series:this.setSeries(json)
        };
       // console.log(option);
        Highcharts.setOptions({
            lang: {printChart:"打印图表",
                downloadJPEG: "下载JPEG 图片" ,
                downloadPDF: "下载PDF文档"  ,
                downloadPNG: "下载PNG 图片"  ,
                downloadSVG: "下载SVG 矢量图" ,
                exportButtonTitle: "导出图片"
            }
        });
        Highcharts.chart(container,option);
    };
}